//表单验证
$(function(){
	function savePay(){
		//判断冻结标记"frezz"
		var buttonFlag = $('#buttonFlag').val();
		var nowFlag = new Date().valueOf();
		if(buttonFlag != null && buttonFlag != ""){
			if(nowFlag - buttonFlag < 2000){
				return;
			}
		}
		//设置冻结
		$('#buttonFlag').val(nowFlag);
		//获取form的值
		var ts = $('#ts').val();
		var orderId = $('#orderId').val();
		var payMethod = $('#payMethod').val();
		var payAccount = $('#payAccount').val();
		var noPayAmount = $('#noPayAmount').val();
		var payMoney = $('#payMoney').val();
		var remark = $('#remark').val();
		
		var buyerCode = $('#buyerCode').val();
		var buyerName = $('#buyerName').val();
		
		var payPicImage = $('#payPicImage').attr("src");
		
		if(orderId == null || orderId == ""){
			layer.msg("订单id为空，请刷新后重试!",{icon:2,time:2000});
			return;
		}
		if(payMethod == null || payMethod == ""){
			layer.msg("请选择支付方式!",{icon:2,time:2000});
			return;
		}
		if(payAccount == null || payAccount == ""){
			layer.msg("请输入支付账号!",{icon:2,time:2000});
			return;
		}
		if(payMoney == null || payMoney == ""){
			layer.msg("请输入支付金额!",{icon:2,time:2000});
			return;
		}
//		if(payMoney > noPayAmount){
//			layer.msg("当前支付金额不能大于该订单待支付金额!",{icon:2,time:2000});
//			return;
//		}
		if(remark != null && remark.length > 512){
			layer.msg("支付备注不能超过512位!",{icon:2,time:2000});
			return;
		}
		
		var pay = {};
		pay["ts"] = ts;
		pay["orderId"] = orderId;
		pay["payMethod"] = payMethod;
		pay["payAccount"] = payAccount;
		pay["payMoney"] = payMoney;
		pay["remark"] = remark;
		pay["payPic"] = payPicImage;
		
		pay["buyerCode"] = buyerCode;
		pay["buyerName"] = buyerName;
		
		$.ajax({
			url:baselocation+'/order/'+orderId+'/pay',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(pay),
			success:function(result){
				$('#savePayBtn').attr("disabled",false);
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					window.location.href=baselocation+'/order/'+result.data.orderId+'/pay/'+result.data.id+'/go-pay';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	}
	$('#savePayBtn').click(function () {
		$('#savePayBtn').attr("disabled",true);
		savePay();
	})
	$('#payMethod_zhifubao').click(function () {
		payCheck('zhifubao','1','支付宝支付：')
	})
	$('#payMethod_weixin').click(function () {
		payCheck('weixin','2','微信支付：')
		var weixinCookie = $.cookie("gou-weixin");
		if(weixinCookie != null && weixinCookie != ''){
			//清空账号
			$('#payAccount').val(weixinCookie);
		}
	})
	$('#payMethod_other').click(function () {
		payCheck('other','3','其他方式支付：')
	})
	function payCheck(payMethodId,payMethod,payMethodName){
		//取消所有选中
		$("input[id^=payMethod_input_]").each(function(){
		    $(this).attr("checked",false);
		});
		//清空账号
		$('#payAccount').val("");
		//赋值支付方式
		$('#payMethod').val(payMethod);
		//选中支付方式
		$('#payMethod_input_'+payMethodId).prop("checked",true);
		//支付金额和方式
		$('#savePayBtn').html(payMethodName+$('#noPayAmount').val()+"元");
	}
	$('#payConfirmBtn').click(function () {
		layer.confirm('点击支付完毕,商家将会收到您的支付消息,请确保您真的完成付款？', {
		    btn: ['确认支付','继续支付'], //按钮
		    icon: 3, 
		    title:'操作提示',
		    skin: 'layui-layer-demo', //样式类名
		    closeBtn: false, //不显示关闭按钮
		    shadeClose: true, //开启遮罩关闭
		    shade: false //不显示遮罩
		}, function(){
			var ts = $('#ts').val();
			var id = $('#id').val();
			var pay = {};
			pay["ts"] = ts;
			pay["id"] = id;
			$.ajax({
				url:baselocation+'/pay/pay-status-update',
				contentType:'application/json',
				type:'post',
				dataType:'json',
				data: JSON.stringify(pay),
				success:function(result){
					if(result.success==false){
						layer.msg(result.message,{icon:2,time:2000});
					}else{
						layer.msg(result.message+"5秒后将为您跳转订单页!",{icon:1,time:4000});
						setTimeout(goToOrderDetails, 4000);
					}
				},
				error:function(result){
					alert("系统异常,请联系管理员！");
				}
			});
		}, function(){
		    //取消
		});
	})
	//跳转list页
	function goToOrderDetails(){
		var orderId = $('#orderId').val();
		window.location.href=baselocation+'/order/'+orderId+'/details';
	}
	
});
var minites = 29;
var seconds = 59;
var showInfoTimes = 3;
function setCountDown(){
	var status = $('#status').val();
	if(status == 0){
		$("#countDownPay").html("剩余支付时间：<span>00</span>时<span>"+minites+"</span>分<span>"+seconds+"</span>秒");
		if( minites == 0 && seconds == 0 ){
			if(showInfoTimes > 0){
				layer.msg("请在24小时内尽快支付，否则将影响您的订单发货!!!",{icon:1,time:4000});
				showInfoTimes --;
			}
		}else if( minites >= 0 ){
			if( seconds > 0 ){
				seconds--;
			}else if( seconds == 0 ){
				minites--;
				seconds = 59;
			}
		}
	}
}
var weixinCookie = $.cookie("gou-weixin");
if(weixinCookie != null && weixinCookie != ''){
	//清空账号
	$('#payAccount').val(weixinCookie);
}